package com.lx.base.config.converter;


import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author lixing
 * @date 2022/6/29 13:38
 */
public class LocalDateTimeJacksonConverter extends JsonDeserializer<LocalDateTime>{

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        DateJacksonConverter dateJacksonConverter=new DateJacksonConverter();
        Date targetDate = dateJacksonConverter.deserialize(p,ctxt);
     if(targetDate==null){
         return null;
     }
        ZoneId zoneId=ZoneId.of("Asia/Shanghai");
        Instant instant=targetDate.toInstant();
        return  LocalDateTime.ofInstant(instant,zoneId);

    }
    @Override
    public Class<?> handledType() {
        return LocalDateTime.class;
    }
}
